

namespace Stee.CAP8.MessageBus.Interface.Utility
{
    using System;

    public static class ValueConverter
    {
        /// <summary>
        /// Convert string to boolean
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static bool ConvertToBoolean(string param, bool defaultValue)
        {
            param = param?.Trim();
            bool value = defaultValue;
            return bool.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to integer
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static int ConvertToInteger(string param, int defaultValue)
        {
            param = param?.Trim();
            int value = defaultValue;
            return int.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to short
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static short ConvertToShort(string param, short defaultValue)
        {
            param = param?.Trim();
            short value = defaultValue;
            return short.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to long
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static long ConvertToLong(string param, long defaultValue)
        {
            param = param?.Trim();
            long value = defaultValue;
            return long.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to float
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static float ConvertToFloat(string param, float defaultValue)
        {
            param = param?.Trim();
            float value = defaultValue;
            return float.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to double
        /// </summary>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static double ConvertToDouble(string param, double defaultValue)
        {
            param = param?.Trim();
            double value = defaultValue;
            return double.TryParse(param, out value) ? value : defaultValue;
        }

        /// <summary>
        /// Convert string to enum
        /// </summary>
        /// <typeparam name="T">Target enumeration type</typeparam>
        /// <param name="param">String parameter</param>
        /// <param name="defaultValue">Default value</param>
        /// <returns>Return converted value</returns>
        public static T ConvertToEnum<T>(string param, T defaultValue)
        {
            param = param?.Trim();
            object value = defaultValue;
            return Enum.TryParse(typeof(T), param, true, out value) ? (T)value : defaultValue;
        }
    }
}
